package com.pts.dao;

import com.pts.db.DbUtils;
import com.pts.vo.Enterprise;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class EnterpriseDao extends GenericDao {
    //通过id 查找企业
    @Override
    public Enterprise findById(int id) {
        //创建输出
        Enterprise enterprise = new Enterprise();
        //数据库查询
        //通过id 查找produce表
        DbUtils du = new DbUtils();
        Connection connection = du.getConn();
        //实例化数据库查询结
        ResultSet resultSet;

        //PreparedStatement 是一个接口 -- 初始化后才能使用


        //查询生产信息
        //编写sql数据库查询语句；
        //通过card查找
        String sql = "select name,place,tel from enterprise where id=?";
        //查找时可能出现SQL异常
        try {
            //准备的陈述
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            //设置查询值
            preparedStatement.setInt(1, id);
            //查询结果
            resultSet = preparedStatement.executeQuery();
            if(!resultSet.next()){
                resultSet.close();
                preparedStatement.close();
                return null;
            }
            //设置相应值
//            product.setId(resultSet.getInt("id"));
            enterprise.setName(resultSet.getString("name"));
            enterprise.setPlace(resultSet.getString("place"));
            enterprise.setTel(resultSet.getString("tel"));
            resultSet.close();
            preparedStatement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return enterprise;
    }

    //查找所有企业
    @Override
    public List<Enterprise> findAll() {
        List list = new ArrayList();
        DbUtils du = new DbUtils();
        Connection connection = du.getConn();
        //实例化数据库查询结
        ResultSet resultSet;

        //PreparedStatement 是一个接口 -- 初始化后才能使用


        //查询生产信息
        //编写sql数据库查询语句；
        //通过card查找
        String sql = "select id,name,place,tel from enterprise";
        //查找时可能出现SQL异常
        try {
            //准备的陈述
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            //设置查询值
            //查询结果
            resultSet = preparedStatement.executeQuery();
            //设置相应值
            //          不仅找而且指向下一个值

            while (resultSet.next()) {
                Enterprise enterprise = new Enterprise();
                enterprise.setId(resultSet.getInt("id"));
                enterprise.setName(resultSet.getString("name"));
                enterprise.setPlace(resultSet.getString("place"));
                enterprise.setTel(resultSet.getString("tel"));
                list.add(enterprise);
            }
            resultSet.close();
            preparedStatement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;

    }


    //添加企业
    @Override
    public boolean add(Object obj) {
        Enterprise enterprise =  (Enterprise) obj;
        DbUtils du = new DbUtils();
        Connection connection = du.getConn();
        String sql = "insert into enterprise(name,place,tel) values (?,?,?)";
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,enterprise.getName());
            preparedStatement.setString(2,enterprise.getPlace());
            preparedStatement.setString(3,enterprise.getTel());
            if(preparedStatement.executeUpdate()==0){
//                System.out.println("添加公司信息异常");
                preparedStatement.close();
                return false;
            }
            preparedStatement.close();
        }
        catch (SQLException e){
            e.printStackTrace();
        }
        return true;
    }

    //修改公司
    @Override
    public boolean update(Object obj) {
        Enterprise enterprise =  (Enterprise) obj;
        DbUtils du = new DbUtils();
        Connection connection = du.getConn();
        String sql = "update enterprise set name = ?,place=?,tel=? where id = ?";
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,enterprise.getName());
            preparedStatement.setString(2,enterprise.getPlace());
            preparedStatement.setString(3,enterprise.getTel());
            preparedStatement.setString(4,String.valueOf(enterprise.getId()));
            if(preparedStatement.executeUpdate()==0){
//                System.out.println("修改公司信息异常");
                preparedStatement.close();
                return false;
            }
            preparedStatement.close();
        }
        catch (SQLException e){
            e.printStackTrace();
        }
        return true;
    }
    @Override
    public boolean delete(int id){
        DbUtils du = new DbUtils();
        Connection connection = du.getConn();
        String sql = "delete from enterprise where id=?";
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,String.valueOf(id));
            if(preparedStatement.executeUpdate()==0){
//                System.out.println("删除公司异常");
                preparedStatement.close();
                return false;
            }
            preparedStatement.close();
        }
        catch (SQLException e){
            e.printStackTrace();
        }
        return true;
    }
    @Override
    public boolean isExist(Object obj) {

        ResultSet resultSet;
        DbUtils du = new DbUtils();
        Connection connection = du.getConn();
        String sql="select id from enterprise where id = ?";
        try {
            PreparedStatement preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1,String.valueOf(obj));
            resultSet = preparedStatement.executeQuery();
            if(resultSet.next()){
                resultSet.close();
                preparedStatement.close();
                return true;
            }
            resultSet.close();
            preparedStatement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }



}
